using System.Text.Json;
using Mathmall.Administration.Mapper;
using Mathmall.Administration.Models;

namespace Mathmall.Administration.Service.impl;

public class CategoryBrandRelationService : ICategoryBrandRelationService
{
    private readonly CategoryBrandRelationMapper _categoryBrandRelationMapper;

    public CategoryBrandRelationService(CategoryBrandRelationMapper categoryBrandRelationMapper)
    {
        _categoryBrandRelationMapper = categoryBrandRelationMapper;
    }

    /// <summary>
    /// 新增分类与品牌的关联关系
    /// </summary>
    /// <param name="categoryBrandRelationVo"></param>
    /// <returns></returns>
    public async Task<int> AddCategoryBrandRelationAsync(CategoryBrandRelationVo categoryBrandRelationVo)
    {
        CategoryBrandRelation categoryBrandRelation =
            JsonSerializer.Deserialize<CategoryBrandRelation>(JsonSerializer.Serialize(categoryBrandRelationVo))!;
        return await _categoryBrandRelationMapper.CreateCategoryBrandRelation(categoryBrandRelation);
    }

    /// <summary>
    /// 根据品牌id查询关联的全部分类
    /// </summary>
    /// <param name="brandID"></param>
    /// <returns></returns>
    public async Task<List<CategoryBrandRelationVo>> GetCategoryBrandRelations(int brandID)
    {
        List<CategoryBrandRelation> categoryBrandRelations =
            await _categoryBrandRelationMapper.GetCategoryBrandRelations(brandID);
        List<CategoryBrandRelationVo> categoryBrandRelationVos =
            JsonSerializer.Deserialize<List<CategoryBrandRelationVo>>(
                JsonSerializer.Serialize(categoryBrandRelations))!;
        return categoryBrandRelationVos;
    }

    /// <summary>
    /// 删除品牌与分类的关联
    /// </summary>
    /// <param name="categoryBrandRelationID"></param>
    /// <returns></returns>
    public async Task<int> DeleteCategoryBrandRelation(int categoryBrandRelationID)
    {
        return await _categoryBrandRelationMapper.DeleteCategoryBrandRelation(categoryBrandRelationID);
    }

    /// <summary>
    /// 更新品牌名称
    /// </summary>
    /// <param name="brandID"></param>
    /// <param name="brandName"></param>
    /// <returns></returns>
    public async Task<int> UpdateBrandName(int brandID, string brandName)
    {
        return await _categoryBrandRelationMapper.UpdateBrandName(brandID, brandName);
    }

    /// <summary>
    /// 更新分类名称
    /// </summary>
    /// <param name="categoryID"></param>
    /// <param name="categoryName"></param>
    /// <returns></returns>
    public async Task<int> UpdateCategoryName(int categoryID, string categoryName)
    {
        return await _categoryBrandRelationMapper.UpdateCategoryName(categoryID, categoryName);
    }
}